import {request} from "@umijs/max";

/** 业务界面列表 GET /api/rule */
export async function businessPageList(
  params:  { current?:number,pageSize?:number,keyword?:string },
  sort:any,
  options?: { [key: string]: any },
) {
  const {data,success} = await request<any>('/wmsServer/businessPage/list', {
    method: 'GET',
    params: {
      ...params
    },
    ...(options || {}),
  });
  console.log("businessPageList--------------->",params,sort,options,data,success)
  return {
    data: data.records,
    // success 请返回 true，
    // 不然 table 会停止解析数据，即使有数据
    success: success,
    // 不传会使用 data 的长度，如果是分页一定要传
    total: data.total,
  }
}

//新增业务界面
export async function addBusinessPage(params:API.BusinessPage){
  return request<any>('/wmsServer/businessPage/add',{
    method:'POST',
    data:{
      ...params
    }
  })
}

//修改业务界面
export async function editBusinessPage(params:API.BusinessPage){
  return request<any>('/wmsServer/businessPage/edit',{
    method:'PUT',
    data:{
      ...params
    }
  })
}

//删除业务页面
export async function deleteBusinessPage(id:string){
  return request<any>('/wmsServer/businessPage/delete',{
    method:'DELETE',
    params:{
      id:id
    }
  })
}

//请求schema
export async function requestContent(id:string|undefined){
  return request<any>('/wmsServer/businessPage/content',{
    method:'GET',
    params:{
      id:id
    }
  })
}

export async function saveContent(params:{id:string,content:string}){
  return request<any>('/wmsServer/businessPage/content',{
    method:'PUT',
    data:{
      ...params
    }
  })
}
//删除页面sql配置信息
export async function deletePageSqlConfig(id:string){
  return request<any>('/wmsServer/pageSqlConfig/delete',{
    method:'DELETE',
    params:{
      id:id
    }
  })
}

export async function pageSqlConfigList(
  params:  { current?:number,pageSize?:number },
  sort:any,
  options?: { [key: string]: any },
) {
  console.log("params-------------",params)
  const {data,success} = await request<any>('/wmsServer/pageSqlConfig/list', {
    method: 'GET',
    params: {
      ...params,
      sortField: Object.keys(sort)[0],
      sortOrder: Object.values(sort)[0],
    },
    ...(options || {}),
  });
  return {
    data: data.records,
    // success 请返回 true，
    // 不然 table 会停止解析数据，即使有数据
    success: success,
    // 不传会使用 data 的长度，如果是分页一定要传
    total: data.total,
  }
}

/**
 * 规则组件脚本列表
 * @param params
 * @param sort
 * @param options
 */
export async function ruleScriptNodeList(
  params:  { current?:number,pageSize?:number },
  sort:any,
  options?: { [key: string]: any },
) {
  const {result,success} = await request<any>('/wmsServer/ruleScriptNode/list', {
    method: 'GET',
    params: {
      ...params,
      sortField: Object.keys(sort)[0],
      sortOrder: Object.values(sort)[0],
    },
    ...(options || {}),
  });
  return {
    data: result.records,
    // success 请返回 true，
    // 不然 table 会停止解析数据，即使有数据
    success: success,
    // 不传会使用 data 的长度，如果是分页一定要传
    total: result.total,
  }
}

//删除页面sql配置信息
export async function deleteRuleScriptNode(id:number){
  return request<any>('/wmsServer/ruleScriptNode/delete',{
    method:'DELETE',
    params:{
      id:id
    }
  })
}

/**
 * 规则组件脚本列表
 * @param params
 * @param sort
 * @param options
 */
export async function ruleChainList(
  params:  { current?:number,pageSize?:number },
  sort:any,
  options?: { [key: string]: any },
) {
  const {result,success} = await request<any>('/wmsServer/ruleChain/list', {
    method: 'GET',
    params: {
      ...params,
      sortField: Object.keys(sort)[0],
      sortOrder: Object.values(sort)[0],
    },
    ...(options || {}),
  });
  return {
    data: result.records,
    // success 请返回 true，
    // 不然 table 会停止解析数据，即使有数据
    success: success,
    // 不传会使用 data 的长度，如果是分页一定要传
    total: result.total,
  }
}

//删除页面sql配置信息
export async function deleteRuleChain(id:number){
  return request<any>('/wmsServer/ruleChain/delete',{
    method:'DELETE',
    params:{
      id:id
    }
  })
}

//全局刷新规则
export async function globalReloadRule(){
  return request<any>('/wmsServer/ruleChain/globalReload',{
    method:'post'
  })
}

//刷新组件脚本
export async function reloadScriptNode(params:API.RuleScriptNode){
  return request<any>('/wmsServer/ruleScriptNode/reload/'+params.id,{
    method:'post'
  })
}

//刷新规则
export async function reloadRuleChain(params:API.RuleChain){
  return request<any>('/wmsServer/ruleChain/reload/'+params.id,{
    method:'post'
  })
}

//新增SQL配置信息
export async function addPageSqlConfig(params:API.PageSqlConfig){
  return request<any>('/wmsServer/pageSqlConfig/add',{
    method:'POST',
    data:{
      ...params
    }
  })
}

//修改SQL配置信息
export async function editPageSqlConfig(params:API.PageSqlConfig){
  return request<any>('/wmsServer/pageSqlConfig/edit',{
    method:'PUT',
    data:{
      ...params
    }
  })
}

export async function pageSqlColumnList(
  params:  { sqlConfigId:string },
  options?: { [key: string]: any }) {
  const {data} = await request<any>('/wmsServer/pageSqlColumn/list', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
  console.log('pageSqlColumnList---------------',data)
  return {
    data: data,
  }
}
